var loginIntervalState = true;
var login = {
    //倒计时
    verificationCodeClick:function(phone,view){
      if(phone.length == 0){
        common.getShowViewError(10,'请输入手机号码');
      }else if (phone.length == 11) {
        var time = 30;
        view.innerHTML=(time+"秒后可重新获取");
        loginIntervalState = false;
        var interval = setInterval(function(){
           time--;
               console.log('....time........'+time);
           if(time == 0){
               clearInterval(interval);
               loginIntervalState = true;
               view.innerHTML=("重新获取");
           }else {
              view.innerHTML=(time+"秒后可重新获取");
           }
        },1000);
      }else {
          common.getShowViewError(10,'手机号码是十一位');
      }
    },
    //请求验证码
    requestVerificationCode:function(phone,authCodeType){
        if(phone.length == 0){
          common.getShowViewError(10,'请输入手机号码');
        }else if (phone.length == 11) {
          var parame = {
           phone:phone,
           authCodeType:authCodeType
          }
          var parames = common.getJSONParame(NewFiData.keyword.phoneVerification,JSON.stringify(parame));
          common.getConsole('验证码','parame...'+JSON.stringify(parame));
          common.ajax($api.getStorage(NewFiData.appParameKey.appFWQ),parames,8,function(ret,err){
            if(ret){
                  common.getConsole('验证码结果','ret...'+JSON.stringify(ret));
                  if(ret.code == 1){
                      if(authCodeType == 0){
                          common.getShowViewError(10,'短信已发送，注意查收');
                      }else {
                          common.getShowViewError(10,'马上电话联系您，请注意');
                      }
                  }else {
                    common.getShowViewError(ret.code,ret.msg);
                  }
            }else {
            common.getShowViewError(err.statusCode,err.msg);
            }
          });
        }else {
            common.getShowViewError(10,'手机号码是十一位');
        }
    },
    //自动登录
    automaticLogin:function(view){
      var token = $api.getStorage(NewFiData.appParameKey.userToken);
      var userid = $api.getStorage(NewFiData.appParameKey.userId);
      var jumd = 1;
      if(token == undefined ||  token == '' || token == 'null'){token = 'null';jumd = 0;}
      if(userid == undefined ||  userid == '' || userid == 'null'){userid = '0'}
      var parame = {
        soleCode:api.deviceId,
        ver:api.appVersion,
        deviceType:api.deviceName,
        deviceVer:api.deviceModel,
        ip:$api.getStorage(NewFiData.wifiKey.wifiIp),
        mac:$api.getStorage(NewFiData.wifiKey.phoneMac),
        userid:userid,
        token:token
      }
      var parames = common.getJSONParame(NewFiData.keyword.visitorLogin,JSON.stringify(parame));
      common.getConsole('自动登录','parame...'+JSON.stringify(parame)+'..fwq..'+$api.getStorage(NewFiData.appParameKey.appFWQ));
      common.ajax($api.getStorage(NewFiData.appParameKey.appFWQ),parames,8,function(ret,err){
        if(ret){
              common.getConsole('自动登录结果','parame...'+JSON.stringify(ret));
              if(!view == undefined || !view == ''){view.style.display = 'none';}
              if(ret.code == 1){
                  //common.applyUI(ret.ui,ret.data,parame.templateClass,parame.templateHtmlBox);
                  if(ret.token == 'null' || jumd == 0){
                    login.getpenWin('loginApp','./html/layout/loginApp.html');
                  }else {
                    $api.setStorage(NewFiData.appParameKey.userId,ret.userid);
                    $api.setStorage(NewFiData.appParameKey.userToken,ret.token);
                    $api.setStorage(NewFiData.user.nickname,ret.nickname);
                    $api.setStorage(NewFiData.user.sex,ret.sex);
                    $api.setStorage(NewFiData.user.pwd,ret.pwd);
                    $api.setStorage(NewFiData.user.selfdom,ret.selfdom);
                    $api.setStorage(NewFiData.user.headimg,ret.headimg);
                    $api.setStorage(NewFiData.user.isLogUploads,ret.isLogUploads);
                    $api.setStorage(NewFiData.user.niuNum,ret.niuNum);
                    login.getpenWin('homePage','./html/layout/homePage.html');
                    //login.getpenWin('loginApp','./html/layout/loginApp.html');
                  }

              }else {
                login.getpenWin('loginApp','./html/layout/loginApp.html');
              }
        }else {
          common.getConsole('自动登录失败','code..'+err.statusCode+'..msg..'+err.msg+'..fwq..'+$api.getStorage(NewFiData.appParameKey.appFWQ));
          login.getpenWin('loginApp','./html/layout/loginApp.html');
        }
      })
    },
    //登录App
    entryApp:function(phone,password){
      var authcode = '';
      if(phone.length !=11){
            common.getShowViewError(10,'请输入正确的手机号');
            return;
      }
      if(password.length <4 || password.length >12){
           common.getShowViewError(10,'密码或验证码是4到12位');
           return;
      }

      if(password.length == 5){
            authcode = password;
            password = '';
      }

      common.getApiShowProgress();
      var parame = {
        soleCode:api.deviceId,
        ver:api.appVersion,
        deviceType:api.deviceName,
        deviceVer:api.deviceModel,
        ip:$api.getStorage(NewFiData.wifiKey.wifiIp),
        mac:$api.getStorage(NewFiData.wifiKey.phoneMac),
        accountType:1,
        account:phone,
        pwd:password,
        authcode:authcode
      }
      var parames = common.getJSONParame(NewFiData.keyword.accountEnroll,JSON.stringify(parame));
      common.getConsole('登录','parame...'+JSON.stringify(parame));
      common.ajax($api.getStorage(NewFiData.appParameKey.appFWQ),parames,8,function(ret,err){
        if(ret){
              common.getConsole('登录结果','parame...'+JSON.stringify(ret));
              common.getApiHideProgress();
              if(ret.code == 1){
                //common.applyUI(ret.ui,ret.data,parame.templateClass,parame.templateHtmlBox);
                   if(ret.status == 1){
                     $api.setStorage(NewFiData.appParameKey.userId,ret.userid);
                     $api.setStorage(NewFiData.appParameKey.userToken,ret.token);
                     $api.setStorage(NewFiData.user.nickname,ret.nickname);
                     $api.setStorage(NewFiData.user.sex,ret.sex);
                     $api.setStorage(NewFiData.user.pwd,ret.pwd);
                     $api.setStorage(NewFiData.user.selfdom,ret.selfdom);
                     $api.setStorage(NewFiData.user.headimg,ret.headimg);
                     $api.setStorage(NewFiData.user.isLogUploads,ret.isLogUploads);
                     $api.setStorage(NewFiData.user.niuNum,ret.niuNum);
                     login.getpenWin('homePage','../../html/layout/homePage.html');
                   }else {
                      common.getShowViewError(10,'status:'+ret.status+'请重新登录')
                   }
              }else {
                common.getShowViewError(ret.code,ret.msg);
              }
        }else {
              common.getConsole('登录失败','err...'+JSON.stringify(err)+'..FWQ..'+$api.getStorage(NewFiData.appParameKey.appFWQ));
              seekAgent.startSeekAgent(function(agentFwq,ret){
                common.getConsole('找代理结果','fwq'+agentFwq+JSON.stringify(ret));
                common.getApiHideProgress();
                if(agentFwq){
                  if(ret.code == 1 || ret.code == 0){
                    $api.setStorage(NewFiData.wifiKey.agentFWQ,agentFwq);
                    $api.setStorage(NewFiData.appParameKey.appFWQ,agentFwq);
                    $api.setStorage(NewFiData.appParameKey.siteAreaid,ret.siteAreaid);
                    common.getShowViewError(10,'登录失败请重新登录')
                  }else {
                  common.getShowViewError(21,ret.msg);
                  }
                }else {
                  common.getShowViewError(11,'没找到代理');
                }
              });
        }
      });
    },
    //跳网页
    getpenWin:function(name,url){
      api.openWin({
          name: name,
          url: url,
          pageParam: {
              name: 'test'
          }
      });
    }
};
